package com.njlime.cms.repository.app;

import com.njlime.cms.entities.LiveComment;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Repository
public class AppLiveCommentDaoImpl extends BaseDaoImpl<LiveComment> {

    /**
     * 详情
     * @param id
     * @return
     */
    public Map<String, Object> get(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_live where id  = ? ");
        arrList.add(id);
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 评论列表
     * @param pageBean
     * @param info
     * @return
     */
    public PageBean list(PageBean pageBean, LiveComment info) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select rc.*,v.head_url,v.mobile from t_live_comment rc ");
        sb.append("left join t_member v on v.id = rc.user_id ");
        sb.append(" where rc.is_del != 1");
        sb.append(" and rc.live_id = ?");
        arrList.add(info.getLive_id());
        if(info.getIs_admin() != null){
            sb.append(" and is_admin = ? ");
            arrList.add(info.getIs_admin());
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by rc.is_top desc,rc.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 点赞数
     * @param id
     */
    public void fabulousNumUpdate(Integer id, int num){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_live_comment SET fabulous_num = fabulous_num + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }
}
